﻿using BaiTu.Framework.MVCFramework.WorkContext;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaiTu.Framework.MVCFramework.Themes
{
    /// <summary>
    /// Theme context
    /// </summary>
    public partial class ThemeContext : IThemeContext
    {
        private readonly IWorkContext _workContext;
        //private readonly IStoreContext _storeContext;
        //private readonly IGenericAttributeService _genericAttributeService;
        //private readonly StoreInformationSettings _storeInformationSettings;
        private readonly IThemeProvider _themeProvider;

        private bool _themeIsCached;
        private string _cachedThemeName;

         public ThemeContext(IWorkContext workContext,
        //    IStoreContext storeContext,
        //    IGenericAttributeService genericAttributeService,
        //    StoreInformationSettings storeInformationSettings,
            IThemeProvider themeProvider)
        {
            this._workContext = workContext;
            //this._storeContext = storeContext;
            //this._genericAttributeService = genericAttributeService;
            //this._storeInformationSettings = storeInformationSettings;
            this._themeProvider = themeProvider;
        }

        /// <summary>
        /// Get or set current theme system name
        /// </summary>
        public string WorkingThemeName
        {
            get
            {
                if (_themeIsCached)
                    return _cachedThemeName;

                string theme = "";
                //if (_storeInformationSettings.AllowCustomerToSelectTheme)
                //{
                //    if (_workContext.CurrentCustomer != null)
                //        theme = _workContext.CurrentCustomer.GetAttribute<string>(SystemCustomerAttributeNames.WorkingThemeName, _genericAttributeService, _storeContext.CurrentStore.Id);
                //}

                ////default store theme
                //if (string.IsNullOrEmpty(theme))
                //    theme = _storeInformationSettings.DefaultStoreTheme;

                //ensure that theme exists
                if (!_themeProvider.ThemeConfigurationExists(theme))
                {
                    var themeInstance = _themeProvider.GetThemeConfigurations()
                        .FirstOrDefault();
                    if (themeInstance == null)
                        throw new Exception("No theme could be loaded");
                    theme = themeInstance.ThemeName;
                }

                //cache theme
                this._cachedThemeName = theme;
                this._themeIsCached = true;
                return theme;
            }
            set
            {
                //if (!_storeInformationSettings.AllowCustomerToSelectTheme)
                //    return;

                //if (_workContext.CurrentCustomer == null)
                //    return;

                //_genericAttributeService.SaveAttribute(_workContext.CurrentCustomer, SystemCustomerAttributeNames.WorkingThemeName, value, _storeContext.CurrentStore.Id);

                //clear cache
                this._themeIsCached = false;
            }
        }
    }
}
